// pages/enter/enter.js
var app=getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    pics: [],
    pics_2:[],
    pics_3: [],
    pics_4: [],
    onload: "block",
    onload_2: "block",
    onload_3: "block",
    onload_4: "block",
    haswrite:0,
    uploadUrl:app.uploadUrl,
    shenq: false,  //审核页面
  },
  // 企业logo
  gotoShow: function () {//这里是选取图片的方法
    var that = this;
    var pics = this.data.pics;
    wx.chooseImage({
      count: 1, // 最多可以选择的图片张数，默认9
      sizeType   : ['original', 'compressed'], // original 原图，compressed 压缩图，默认二者都有
      sourceType : ['album', 'camera'], // album 从相册选图，camera 使用相机，默认二者都有
      success    : function (res) {
        wx.showLoading({ title: '上传中' })
        wx.uploadFile({
          url      : app.url + '/business/upload',
          filePath : res.tempFilePaths[0],
          name     : 'file',
          header   : { "Content-Type": "multipart/form-data" },
          success  : function (ress) {
            var ress = JSON.parse(ress.data);
            if(ress.code==1){
              pics[0] = ress.src
              that.setData({ pics: pics });
            }else{
              wx.showToast({
                title: '上传失败',
                icon:'none'
             })
            }
            wx.hideLoading();
          }
        })   
      },
      fail: function () {
        // fail
      },
      complete: function () {
        // complete
      }
    })
  },
  deleteList(e) {
    const index = e.currentTarget.dataset.index;
    let pics    = this.data.pics;
    pics.splice(index, 1);
    this.setData({ pics: pics });
    if (pics.length < 4) { this.setData({ onload: "block" });}
  },
  // 营业执照
  gotoShow_2: function () {//这里是选取图片的方法
    var that   = this;
    var pics_2 = this.data.pics_2;
    wx.chooseImage({
      count      : 3, // 最多可以选择的图片张数，默认9
      sizeType   : ['original', 'compressed'], // original 原图，compressed 压缩图，默认二者都有
      sourceType : ['album', 'camera'], // album 从相册选图，camera 使用相机，默认二者都有
      success    : function (res) {
        wx.showLoading({ title: '上传中', })
        wx.uploadFile({
          url     : app.url + '/business/upload',
          filePath: res.tempFilePaths[0],
          name    : 'file',
          header  : { "Content-Type": "multipart/form-data" },
          success : function (e) {
            if (pics_2.length < 3) {that.setData({onload: "", });
              pics_2.push(JSON.parse(e.data).src)
              that.setData({  pics_2: pics_2 })
            } else {
              wx.hideLoading();
              that.setData({ onload: "none",});
              return false;
            }
            wx.hideLoading();
          }
        })
      },
      fail: function () {
        // fail
      },
      complete: function () {
        // complete
      }
    })
  },
  deleteList_2(e) {
    const index = e.currentTarget.dataset.index;
    let pics_2  = this.data.pics_2;
    pics_2.splice(index, 1);
    this.setData({  pics_2: pics_2 });
    if (pics_2.length < 4) { this.setData({ onload_2: "block",}); }
  },
  // 身份证正面
  gotoShow_3: function () {//这里是选取图片的方法
    var that = this;
    var pics_3 = this.data.pics_3;
    wx.chooseImage({
      count: 1, // 最多可以选择的图片张数，默认9
      sizeType: ['original', 'compressed'], // original 原图，compressed 压缩图，默认二者都有
      sourceType: ['album', 'camera'], // album 从相册选图，camera 使用相机，默认二者都有
      success: function (res) {
        wx.showLoading({ title: '上传中', })
        wx.uploadFile({
          url: app.url + '/business/upload',
          filePath: res.tempFilePaths[0],
          name: 'file',
          header: { "Content-Type": "multipart/form-data" },
          success: function (e) {
            if (pics_3.length < 1) {
              that.setData({ onload: "", });
              pics_3.push(JSON.parse(e.data).src)
              that.setData({ pics_3: pics_3 })
            } else {
              wx.hideLoading();
              that.setData({ onload: "none", });
              return false;
            }
            wx.hideLoading();
          }
        })
      },
      fail: function () {
        // fail
      },
      complete: function () {
        // complete
      }
    })
  },
  deleteList_3(e) {
    const index = e.currentTarget.dataset.index;
    let pics_3 = this.data.pics_3;
    pics_3.splice(index, 1);
    this.setData({ pics_3: pics_3 });
    if (pics_3.length < 4) { this.setData({ onload_3: "block", }); }
  },
  // 人像照
  gotoShow_4: function () {//这里是选取图片的方法
    var that = this;
    var pics_4 = this.data.pics_4;
    wx.chooseImage({
      count: 3, // 最多可以选择的图片张数，默认9
      sizeType: ['original', 'compressed'], // original 原图，compressed 压缩图，默认二者都有
      sourceType: ['album', 'camera'], // album 从相册选图，camera 使用相机，默认二者都有
      success: function (res) {
        wx.showLoading({ title: '上传中', })
        wx.uploadFile({
          url: app.url + '/business/upload',
          filePath: res.tempFilePaths[0],
          name: 'file',
          header: { "Content-Type": "multipart/form-data" },
          success: function (e) {
            if (pics_4.length < 3) {
              that.setData({ onload: "", });
              pics_4.push(JSON.parse(e.data).src)
              that.setData({ pics_4: pics_4 })
            } else {
              wx.hideLoading();
              that.setData({ onload: "none", });
              return false;
            }
            wx.hideLoading();
          }
        })
      },
      fail: function () {
        // fail
      },
      complete: function () {
        // complete
      }
    })
  },
  deleteList_4(e) {
    const index = e.currentTarget.dataset.index;
    let pics_4 = this.data.pics_4;
    pics_4.splice(index, 1);
    this.setData({ pics_4: pics_4 });
    if (pics_4.length < 4) { this.setData({ onload_4: "block", }); }
  },
  // input
  input:function(e){
    let that     = this
    var haswrite = that.data.haswrite
    if (e.detail.value !== "" && haswrite < 6){
      that.setData({ haswrite : haswrite + 1 })
    }
    else if (haswrite > 0){
      that.setData({ haswrite : haswrite - 1 })
    }
    // console.log(haswrite,"--------------------------------")
  },
  // 提交申请
  formSubmit:function(res){
    var that = this;
    var res  = res.detail.value;
    if (that.data.pics.length<1){
      wx.showToast({ title: '请上传门头照片', icon:'none' }); return false;
    }
    if (that.data.pics_2.length<2){
      wx.showToast({ title: '请上传营业执照正副本',icon: 'none' });return false;
    }
    if (that.data.pics_3.length < 1) {
      wx.showToast({ title: '请上传身份证正面', icon: 'none' }); return false;
    } 
    if (that.data.pics_4.length < 1) {
      wx.showToast({ title: '请上传人像照', icon: 'none' }); return false;
    }
    let address =that.data.address;
    let lat     = that.data.lat;
    let lng     = that.data.lng;
    let logo    = that.data.pics[0];
    let licence = that.data.pics_2.join();
    let licence3 = that.data.pics_3.join();
    let licence4 = that.data.pics_4.join();
    let name=res.name;
    let username=res.username;
    let phone=res.phone
    let trade = res.trade
    if (!address || !lat || !lng){
      wx.showToast({  title: '请选择地址', icon: 'none' }); return false;
    }
    if (!(/^1[3|4|5|6|8|7][0-9]\d{4,8}$/.test(phone))){
      wx.showToast({ title: '手机号码错误', icon: 'none' }); return false;
    }
    if (!name || !username || !trade){
      wx.showToast({ title: '信息填写不完整',icon: 'none' }); return false;
    }
    wx.request({
      url: app.url +'/Business/verify',
      method:'post',
      data:{
        uid: wx.getStorageSync('uid'), address, lat, lng, logo, licence, licence3,licence4,name,username,phone,trade
      },
      success:function(res){
        var code = res.data.code;
        if(code == 1){
          wx.showModal({ title: '提示', content: '提交成功，请耐心等待',
            success:function(result){
              if (result.confirm || result.cancel) { wx.navigateBack({delta: 1 }) }
            }
          })
        }else{
          wx.showModal({
            title: '提示', content: '提交失败',
          })
        }
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
   
  },
  open_map:function(){
    var that=this;
    wx.chooseLocation({
      success: function (res) {
        that.setData({
          address:res.address,
          lat:res.latitude,
          lng:res.longitude
        })
      }
    });
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})